<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="商户订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入商户订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          clearable
          v-model="queryParams.payTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择支付时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="选择学校" prop="schoolId">
        <el-select v-model="queryParams.schoolId" placeholder="请选择" clearable>
          <el-option
            v-for="item in schoolList"
            :key="item.id"
            :label="item.schoolName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="选择年级" prop="gradeId">
        <el-select v-model="queryParams.gradeId" placeholder="请选择" clearable>
          <el-option
            v-for="item in gradeList"
            :key="item.id"
            :label="item.gradeName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="班级" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家长姓名" prop="parentName">
        <el-input
          v-model="queryParams.parentName"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家长手机号" prop="parentPhone">
        <el-input
          v-model="queryParams.parentPhone"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- 
      <el-form-item label="收货人手机号" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入收货人手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递单号" prop="expressNumber">
        <el-input
          v-model="queryParams.expressNumber"
          placeholder="请输入快递单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="订单状态" prop="tag">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="请选择"
          clearable
          style="width: 100%"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['order:order:export']">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="订单支付号" align="center" prop="tradeNo" width="150"/>
      <el-table-column label="商户订单号" align="center" prop="orderNo" width="150"/>
      <el-table-column label="订单总价" align="center" prop="totalPrice" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <span v-if="scope.row.orderStatus === 0">待付款</span>
          <span v-if="scope.row.orderStatus === 1">待发货</span>
          <span v-if="scope.row.orderStatus === 2">待领取</span>
          <span v-if="scope.row.orderStatus === 3">已完成</span>
          <span v-if="scope.row.orderStatus === 4">已取消</span>
          <span v-if="scope.row.orderStatus === 5">已退款</span>
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="payPrice" />
      <el-table-column label="支付方式" align="center" prop="payType">
        <template #default="scope">
          <span v-if="scope.row.payType === 0">无</span>
          <span v-if="scope.row.payType === 1">支付宝支付</span>
          <span v-if="scope.row.payType === 2">微信支付</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template #default="scope">
          <span v-if="scope.row.payStatus === 0">未支付</span>
          <span v-if="scope.row.payStatus === 1">支付成功</span>
          <span v-if="scope.row.payStatus === 2">支付失败</span>
        </template>
      </el-table-column>
      <el-table-column label="家长姓名" align="center" prop="parentName" />
      <el-table-column label="家长号码" align="center" prop="parentPhone" />
      <el-table-column label="学校" align="center" prop="schoolName" />
      <el-table-column label="年级" align="center" prop="gradeName" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="学生" align="center" prop="studentName" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="cancelTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center" width="180"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button v-if="scope.row.orderStatus === 2"
            link
            type="primary"
            icon="Edit"
            @click="handleConfirmReceipt(scope.row)"
            v-hasPermi="['order:order:edit']"
            >确认收货</el-button
          >
          <el-button v-if="scope.row.orderStatus !== 0 && scope.row.orderStatus !== 5 && scope.row.orderStatus !== 4"
            link
            type="primary"
            icon="Edit"
            @click="handleRefund(scope.row)"
            v-hasPermi="['order:order:edit']"
            >确认退款</el-button
          >
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleDetail(scope.row)"
            v-hasPermi="['order:order:detail']"
            >详情</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:order:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <orderDetail ref="orderDetailRef"/>
    <confirmReceipt ref="confirmReceiptRef" @success="getList"/>
  </div>
</template>

<script setup name="Orders">
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  refundOrder,
} from "@/api/order/order";

import { listGrades } from "@/api/org/grades";

import { listSchools } from "@/api/org/school.js";

import orderDetail from "@/views/order/order/components/OrderDetail.vue";

import confirmReceipt from "@/views/order/order/components/ConfirmReceipt.vue";

const { proxy } = getCurrentInstance();

// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref([]);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 订单表格数据
const orderList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);
// 订单状态
const order_status = ref([{
  value:0,
  label:'待付款'
},{
  value:1,
  label:'待发货'
},{
  value:2,
  label:'已发货'
},{
  value:3,
  label:'已完成'
},{
  value:4,
  label:'已取消'
},{
  value:5,
  label:'已退款'
}])

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    orderStatus: null,
    payTime: null,
    schoolId: null,
    gradeId: null,
    className: null,
    parentName: null,
    parentPhone: null,
    studentName: null,
  },
});

const { queryParams } = toRefs(data);

/** 查询订单列表 */
const getList = () => {
  loading.value = true;
  listOrder(queryParams.value).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryForm");
  handleQuery();
};

/** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除订单编号为"' + ids + '"的数据项？')
    .then(function () {
      return delOrder(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy.download(
    "admin/order/order/export",
    {
      ...queryParams.value,
    },
    `订单_${new Date().getTime()}.xlsx`
  );
}

/**详情按钮操作 */
const orderDetailRef = ref()
const handleDetail = (row) => {
  orderDetailRef.value.openDialog(row.id)
};

/**确认收货 */
const confirmReceiptRef = ref()
const handleConfirmReceipt = (row) => {
  const id = row.id
  confirmReceiptRef.value.openDialog(id)
}

/**确认退款 */
const handleRefund = (row) => {
  const id = row.id;
  proxy.$modal
    .confirm('是否确认退款？')
    .then(function () {
      return refundOrder({
        id
      });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("退款成功");
    })
    .catch(() => {});
}

const schoolList = ref();
function listSchoolsReq() {
  listSchools({ pageNum: 1, pageSize: 99 }).then((res) => {
    schoolList.value = res.rows || [];
  });
}

const gradeList = ref([]);
function listGradesReq() {
  listGrades({ pageNum: 1, pageSize: 99 }).then((res) => {
    gradeList.value = res.rows || [];
  });
}

listSchoolsReq();

listGradesReq();

getList();
</script>
